package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/internal/course/constant"
	"github.com/lab-online/internal/course/entity"
	mw "github.com/lab-online/pkg/middleware"
	"github.com/lab-online/pkg/resp"
)

type UpdateExperimentReqParam struct {
	ID uint `uri:"id" binding:"required,min=1" example:"1"` // 课程ID
}

type UpdateExperimentReqBody struct {
	Name        string `json:"name" binding:"required,min=1,max=20" example:"高等数学"`         // 实验名称
	Description string `json:"description" binding:"required,min=1,max=100" example:"实验描述"` // 实验介绍
}

var UpdateExperimentValidator = mw.NewValidator(func() *mw.ValidatorOptions {
	return &mw.ValidatorOptions{
		Params: &UpdateExperimentReqParam{},
		Body:   &UpdateExperimentReqBody{},
	}
})

type UpdateExperimentRespBody struct {
	resp.BaseRespBody
}

// UpdateExperiment 更新实验
//
//	@Summary				更新实验
//	@Description			管理员可以更新实验信息
//	@Description.markdown	UpdateCourse
//	@Tags					course
//	@Accept					json
//	@Produce				json
//	@Security				bearer
//	@Param					Authorization	header		string					true	"`admin`"
//	@Param					id				path		uint					true	"实验"
//	@Param					course			body		UpdateExperimentReqBody	true	"更新信息"
//	@Success				201				{object}	UpdateExperimentRespBody
//	@Failure				400				{object}	resp.FailedRespBodyString
//	@Failure				403				{object}	resp.FailedRespBodyString
//	@Failure				500				{object}	resp.FailedRespBodyString
//	@x-apifox-status		"developing"
//	@Router					/v1/experiments/:id	[put]
func (h *Handler) UpdateExperiment(c *gin.Context) {
	id := c.MustGet(mw.KeyParams).(*UpdateExperimentReqParam).ID
	body := c.MustGet(mw.KeyBody).(*UpdateExperimentReqBody)

	experimentEntity := entity.NewExperimentEntity(
		entity.WithExpID(id),
		entity.WithExpName(body.Name),
		entity.WithExpDescription(body.Description),
	)

	if err := h.domain.UpdateExperiment(experimentEntity); err != nil {
		resp.ErrorHandler(c, err)
	} else {
		resp.Success[any](c, constant.CodeUpdateExperimentSuccess)
	}
}
